package com.javabi.common.concurrent.collect.copyonwrite;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

public final class CopyOnWrite {

	public static <E> CopyOnWriteArrayList<E> newArrayList() {
		return new CopyOnWriteArrayList<E>();
	}

	public static <E> CopyOnWriteArraySet<E> newArraySet() {
		return new CopyOnWriteArraySet<E>();
	}

	public static <E> List<E> newLinkedList() {
		return new CopyOnWriteList<E>() {

			@Override
			protected List<E> newList(List<E> list) {
				return new LinkedList<E>(list);
			}
		};
	}

	public static <E> Set<E> newHashSet() {
		return new CopyOnWriteSet<E>() {

			@Override
			protected Set<E> newSet(Set<E> set) {
				return new HashSet<E>(set);
			}
		};
	}

	public static <E> Set<E> newTreeSet() {
		return new CopyOnWriteSet<E>() {

			@Override
			protected Set<E> newSet(Set<E> set) {
				return new TreeSet<E>(set);
			}
		};
	}

	public static <K, V> CopyOnWriteMap<K, V> newHashMap() {
		return new CopyOnWriteMap<K, V>() {

			@Override
			protected Map<K, V> newMap(Map<K, V> map) {
				return new HashMap<K, V>(map);
			}
		};
	}

	public static <K, V> CopyOnWriteMap<K, V> newLinkedHashMap() {
		return new CopyOnWriteMap<K, V>() {

			@Override
			protected Map<K, V> newMap(Map<K, V> map) {
				return new LinkedHashMap<K, V>(map);
			}
		};
	}

	public static <K, V> CopyOnWriteMap<K, V> newTreeHashMap() {
		return new CopyOnWriteMap<K, V>() {

			@Override
			protected Map<K, V> newMap(Map<K, V> map) {
				return new TreeMap<K, V>(map);
			}
		};
	}

	private CopyOnWrite() {
		// Utility constructor
	}

}
